package com.blue.Impl.system;

import com.blue.mapper.system.GenerateMapper;
import com.blue.service.system.IGenerateService;
import com.blue.common.entity.GenerateSet;
import com.blue.common.utils.GenUtils;
import org.apache.tomcat.util.http.fileupload.IOUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.io.ByteArrayOutputStream;
import java.util.List;
import java.util.Map;
import java.util.zip.ZipOutputStream;

/**
 * @author nanjie.chen
 * Created on 2023/10/2
 */
@Service
public class GenerateServiceImpl implements IGenerateService {
    @Autowired
    private GenerateMapper generateMapper;

    @Override
    public List<Map<String, String>> pageList(String tableName) {
        return generateMapper.queryList(tableName);
    }

    @Override
    public byte[] generateCode(GenerateSet set) {
        ByteArrayOutputStream outputStream = new ByteArrayOutputStream();
        ZipOutputStream zip = new ZipOutputStream(outputStream);
        List<String> tableNames = set.getTableNames();
        for (String tableName : tableNames) {
            //查询表信息
            Map<String, String> table = generateMapper.queryTable(tableName);
            //查询列信息
            List<Map<String, String>> columns = generateMapper.queryColumns(tableName);
            //生成代码
            GenUtils.generatorCode(table, columns, zip, set);
        }
        IOUtils.closeQuietly(zip);
        return outputStream.toByteArray();
    }
}
